﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;

namespace SomeKit.Http
{
    public static partial class HttpHelper
    {
        /// <summary>
        /// Sends an http reqeuest to the endpoint pointed to by the <paramref name="request"/> object. The object has to be prepopulated with all headers necessary before invoking this method.
        /// </summary>
        /// <remarks>
        /// The given handlers can be executed in any thread, meaning that ie. invocation in i UI thread should be incorporated in the given delegates. This applies to <paramref name="resultHandler"/>
        /// as well as <paramref name="errorHandler"/>.
        /// </remarks>
        /// <param name="request">The request object.</param>
        /// <param name="resultHandler">A handler for the response.</param>
        /// <param name="errorHandler">A handler for exceptions.</param>
        public static void SendRequest(this HttpWebRequest request, Action<string> resultHandler = null, Action<Exception> errorHandler = null)
        {
            try
            {
                request.BeginGetResponse(new AsyncCallback(GetResponseCallback),
                    new AsyncState
                    {
                        Request = request,
                        FinalHandler = resultHandler,
                        ErrorHandler = errorHandler
                    });
            }
            catch (Exception e)
            {
                if (errorHandler != null) 
                    errorHandler(e);
            }
        }

        /// <summary>
        /// Sends an http reqeuest to the endpoint pointed to by the <paramref name="request"/> object. The object has to be prepopulated with all headers necessary before invoking this method.
        /// </summary>
        /// <remarks>
        /// The given handlers can be executed in any thread, meaning that ie. invocation in i UI thread should be incorporated in the given delegates. This applies to <paramref name="resultHandler"/>
        /// as well as <paramref name="errorHandler"/>.
        /// </remarks>
        /// <param name="request">The request object.</param>
        /// <param name="body">The body of the request.</param>
        /// <param name="resultHandler">A handler for the response.</param>
        /// <param name="errorHandler">A handler for exceptions.</param>
        public static void SendRequest(this HttpWebRequest request, string body, Action<string> resultHandler = null, Action<Exception> errorHandler = null)
        {
            try
            {
                if (string.IsNullOrEmpty(body))
                    request.SendRequest(resultHandler, errorHandler);
                else request.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback),
                    new AsyncStateWithBody
                    {
                        Request = request,
                        Body = body,
                        FinalHandler = resultHandler,
                        ErrorHandler = errorHandler
                    });
            }
            catch (Exception e)
            {
                if (errorHandler != null) 
                    errorHandler(e);
            }
        }
    }
}
